package com.qinglei.recoup.system.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qinglei.recoup.common.exception.RecoupException;
import com.qinglei.recoup.common.exception.RedisConnectException;
import com.qinglei.recoup.common.service.RedisService;
import com.qinglei.recoup.common.service.impl.TestRadarService;
import com.qinglei.recoup.common.utils.DateTimeUtils;
import com.qinglei.recoup.common.utils.DateUtil;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.system.domain.*;
import com.qinglei.recoup.system.enums.BedStatusEnum;
import com.qinglei.recoup.system.enums.EventEnum;
import com.qinglei.recoup.system.pojo.BedStatusVO;
import com.qinglei.recoup.system.pojo.EventLogVO;
import com.qinglei.recoup.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping("sleepBreath")
@Api(tags = "健康报告数据落地存储数据库")
public class BedSleepBreathImportDataController {

    @Resource
    private TestRadarService testRadarService;

    @Autowired
    private BreathAnalysisService breathAnalysisService;

    @Autowired
    private SleepAnalysisService sleepAnalysisService;

    @Autowired
    private BedSleepBreathImportDataService  bedSleepBreathImportDataService;


    @Resource
    private SystemConfigService systemConfigService;

    @Autowired
    private RadarService radarService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;
    @Autowired
    private RoomService roomService;

    @Autowired
    private VitalSignsAlarmService vitalSignsAlarmService;

    @Autowired
    private RealTimeMonitoringService realTimeMonitoringService;

    @Autowired
    private HealthBigDataService healthBigDataService;

    @Value("${recoup.tenant.theme.default-banner-url}")
    private String defaultBannerUrl;

    @Value("${recoup.tenant.theme.default-banner-color}")
    private String defaultBannerColor;

    @Value("${recoup.tenant.default-system-configs}")
    private String defaultSystemConfigs;

    @Value("${recoup.tenant.default-vital-signs-alarm}")
    private String defaultVitalSignsAlarm;

    @Value("${recoup.tenant.default-real-time-monitoring}")
    private String defaultRealTimeMonitoring;

    @Value("${recoup.tenant.default-health-big-data}")
    private String defaultHealthBigData;


    @ApiOperation(value = "健康报告数据落地存储数据库")
    @GetMapping("sleepBreathData")
    public String SleepBreathData(String day)  {
        LocalDate yesterday = LocalDate.now().minusDays(1);
        if(StringUtils.isNotEmpty(day)){
            yesterday = DateTimeUtils.yyyy_MM_dd(day);
        }
        List<BreathAnalysis> breathAnalysisList = breathAnalysisService.getBreathAnalysisList(yesterday);
        List<SleepAnalysis> sleepAnalysisList = sleepAnalysisService.getSleepAnalysisList(yesterday);
        bedSleepBreathImportDataService.BedSleepBreathImportData(breathAnalysisList,sleepAnalysisList,yesterday);
        return "ok";
    }


    @GetMapping("test")
    public String importHealthData1() throws RecoupException, RedisConnectException {

        //添加初始化生命体征报警阈值默认值
        List<VitalSignsAlarm>  vitalSignsAlarmList =JsonUtils.jsonToList(defaultVitalSignsAlarm, VitalSignsAlarm.class);
        vitalSignsAlarmList.forEach(f->f.setTenantId(16));
        vitalSignsAlarmService.saveBatch(vitalSignsAlarmList);
        List<VitalSignsAlarm> VitalSignsAlarmList2 = vitalSignsAlarmService.list(Wrappers.<VitalSignsAlarm>lambdaQuery().eq(VitalSignsAlarm::getTenantId, 16));
        VitalSignsAlarmList2.forEach(f->{
            String s = JsonUtils.objectToJson(f);
            try {
                redisService.set("VitalSignsAlarm"+f.getTenantId()+f.getSignType(),s);
            } catch (RedisConnectException e) {
                e.printStackTrace();
            }

        });

        //添加初始化实时监测默认值
        List<RealTimeMonitoring>  realTimeMonitoringList =JsonUtils.jsonToList(defaultRealTimeMonitoring, RealTimeMonitoring.class);
        realTimeMonitoringList.forEach(f->f.setTenantId(16));
        realTimeMonitoringService.saveBatch(realTimeMonitoringList);
        List<RealTimeMonitoring> RealTimeMonitoringList2 = realTimeMonitoringService.list(Wrappers.<RealTimeMonitoring>lambdaQuery().eq(RealTimeMonitoring::getTenantId, 16));
        String s1 = JsonUtils.objectToJson(RealTimeMonitoringList2);
        redisService.set("RealTimeMonitoring"+RealTimeMonitoringList2.get(0).getTenantId(),s1);


        //添加初始化健康大数据阈值默认值
        List<HealthBigData>  healthBigDataList =JsonUtils.jsonToList(defaultHealthBigData, HealthBigData.class);
        healthBigDataList.forEach(f->f.setTenantId(16));
        healthBigDataService.saveBatch(healthBigDataList);
        List<HealthBigData> HealthBigDataList2 = healthBigDataService.list(Wrappers.<HealthBigData>lambdaQuery().eq(HealthBigData::getTenantId, 16));
        HealthBigDataList2.forEach(f->{
            String s = JsonUtils.objectToJson(f);
            try {
                redisService.set("HealthBigData"+f.getTenantId()+f.getBigDataType(),s);
            } catch (RedisConnectException e) {
                e.printStackTrace();
            }

        });
        return "OK";
    }
    @GetMapping("test2")
    public   String  isInteger() {
        List<Room> list = roomService.list();
        System.out.println(list);
        list.forEach(f->
                {if (f.getName() != null && !"".equals(f.getName())) {
                    Pattern pattern = Pattern.compile("[0-9]*");
                    boolean matches = pattern.matcher(f.getName()).matches();
                    StringBuilder strb = new StringBuilder();
                    if (matches) {
                        int length = f.getName().length();
                        int a = 4;
                        if (length < a) {
                            for (int i = 0; i < a - length; i++) {
                                strb.append("0");
                            }
                            strb.append(f.getName());
                            String s = strb.toString();
                            UpdateWrapper<Room> updateWrapper = new UpdateWrapper<>();
                            updateWrapper.eq("id",f.getId()).set("name", s);
                            roomService.update(updateWrapper);
                        }
                    }

                }}

        );
        return "OK";
    }

}
